﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web.UI.WebControls;
using JUtil.CodeGenerator;
using JUtil.Web.Extensions;

namespace ProjectDailyReport
{
    internal sealed class MessageBody : CodeGenBase
    {
        List<ProjectProgress> lstProjectProgress = new List<ProjectProgress>();

        public MessageBody(ProjectProgress ProjectProgress)
        {
            lstProjectProgress.Add(ProjectProgress);
        }

        public MessageBody(List<ProjectProgress> lstProjectProgress)
        {
            this.lstProjectProgress.AddRange(lstProjectProgress);
        }

        #region TableContent
        private string TableContent
        {
            get
            {
                string block = GetTemplate("TableContent");

                Hashtable variables = new Hashtable();
                variables["HeadContent"] = HeadContent;
                variables["CellContents"] = CellContents;

                block = ReplaceBlock(block, variables);

                return block;
            }
        }

        private string HeadContent
        {
            get
            {
                TableHeaderRow thr = new TableHeaderRow();

                Type typeProjectProgress = typeof(ProjectProgress);
                foreach (PropertyInfo pi in typeProjectProgress.GetProperties())
                {
                    TableHeaderCell thc = new TableHeaderCell();
                    thc.Text = pi.Name;

                    object[] propertyComments = pi.GetCustomAttributes(typeof(PropertyCommentAttribute), true);

                    if (propertyComments == null || propertyComments.Length == 0)
                        thc.Text = pi.Name;
                    else
                    {
                        PropertyCommentAttribute PropertyComment = propertyComments[0] as PropertyCommentAttribute;
                        thc.Text = PropertyComment.Comment;
                    }

                    thr.Controls.Add(thc);
                }

                return thr.ToHtml();
            }
        }

        private string CellContents
        {
            get
            {
                StringBuilder output = new StringBuilder();
                foreach (ProjectProgress ProjectProgress in lstProjectProgress)
                {
                    output.AppendFormat("{0}\n", CellContent(ProjectProgress));
                }
                return output.ToString();
            }
        }

        private string CellContent(ProjectProgress ProjectProgress)
        {
            TableRow tr = new TableRow();

            Type typeProjectProgress = ProjectProgress.GetType();
            foreach (PropertyInfo pi in typeProjectProgress.GetProperties())
            {
                object value = pi.GetValue(ProjectProgress, null);

                value = value ?? string.Empty;

                TableCell td = new TableCell();
                td.Text = value.ToString();

                tr.Controls.Add(td);
            }

            return tr.ToHtml();
        }
        #endregion

        #region OverrideMethods
        protected override void AssignExportTemplate(System.Collections.Hashtable Variables)
        {
            Variables["TableContent"] = TableContent;
        }

        protected override string GetExportTemplate()
        {
            return GetTemplate("ExportTemplate");
        }

        public override string ToString()
        {
            return Export();
        }
        #endregion
    }
}
